using System.Text.RegularExpressions;

namespace CSharpPro._1016;


public class Workfive
{
    public void TestPrint()
    {


        Console.Write("输入三个数 (用空格分隔): ");
        string[] inputs = Console.ReadLine().Split(' ');

        int a = int.Parse(inputs[0]);
        int b = int.Parse(inputs[1]);
        int c = int.Parse(inputs[2]);


        if (a > b) (a, b) = (b, a);
        if (b > c) (b, c) = (c, b);
        if (a > b) (a, b) = (b, a);

        // 输出
        Console.WriteLine($"排序结果: {a} {b} {c}");

    }


    public void TestPrint2()
    {
        int sum = 0;
        int count = 0;
        Console.WriteLine("输出1-100之间所有不是7的倍数和不包含7的数字");
        for (int i = 0; i < 100; i++)
        {
            if (i % 7 == 0)
            {
                continue;
            }
            else
            {
                if ((i / 10 == 7) || (i % 10 == 7))
                {
                    continue;
                }
                else
                {
                    Console.Write(i + "");
                    sum += i;
                    count++;

                }
            }

            if (count % 10 == 0)
            {
                Console.WriteLine();
            }


        }

        Console.WriteLine($"\n符合条件的数字总和: {sum}");
        Console.WriteLine($"\n符合条件的数字: {count}");
    }

    public void TestPrint3()
    {
        Console.WriteLine("打印出九九乘法表");
        for (int i = 1; i <= 9; i++)
        {
            for (int j = 1; j < 10; j++)
            {
                Console.Write("j");
            }
        }
    }

    public void TestPrint4()
    {


        Console.WriteLine("1000以内的完数有：");

        for (int i = 2; i <= 1000; i++)
        {
            int sum = 0;

            // 找出所有真因子并求和
            for (int j = 1; j <= i / 2; j++)
            {
                if (i % j == 0)
                {
                    sum += j;
                }
            }

            // 如果真因子之和等于该数本身，则是完数
            if (sum == i)
            {
                Console.Write($"{i} = ");

                // 输出因子分解式
                for (int j = 1; j <= i / 2; j++)
                {
                    if (i % j == 0)
                    {
                        Console.Write(j);
                        if (j < i / 2 && i % (j + 1) == 0)
                            Console.Write(" + ");
                    }
                }

                Console.WriteLine();
            }
        }

    }

    public void TestPrint5()
    {

        static void Main()
        {
            Console.Write("请输入一个正整数：");
            int num = int.Parse(Console.ReadLine());

            // 判断是几位数
            string numStr = num.ToString();
            Console.WriteLine("它是{numStr.Length}位数");

            // 逆序打印各位数字
            Console.Write("逆序打印：");
            for (int i = numStr.Length - 1; i >= 0; i--)
            {
                Console.Write(numStr[i] + " ");
            }

            Console.WriteLine();

            // 或者使用数学方法
            Console.Write("数学方法逆序：");
            int temp = num;
            while (temp > 0)
            {
                int digit = temp % 10;
                Console.Write(digit + " ");
                temp /= 10;
            }

            Console.WriteLine();
        }

    }

    public void TestPrint6()
    {
        Console.Write("请输入菱形的行数(奇数): ");
        int rows = int.Parse(Console.ReadLine());

        if (rows % 2 == 0)
        {
            Console.WriteLine("请输入奇数！");
            return;
        }

        int mid = rows / 2;

        // 上半部分
        for (int i = 0; i <= mid; i++)
        {
            Console.WriteLine(new string(' ', mid - i) + new string('*', 2 * i + 1));
        }

        // 下半部分
        for (int i = mid - 1; i >= 0; i--)
        {
            Console.WriteLine(new string(' ', mid - i) + new string('*', 2 * i + 1));
        }


    }

    
    private static Dictionary<string, string> users = new Dictionary<string, string>();
    
    static void Main(string[] args)
    {
        ShowMainMenu();
    }
    
    static void ShowMainMenu()
    {
        while (true)
        {
            Console.WriteLine("\n=== 用户管理系统 ===");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 退出");
            Console.Write("请选择: ");
            
            string choice = Console.ReadLine();
            
            switch (choice)
            {
                case "1":
                    Login();
                    break;
                case "2":
                    Register();
                    break;
                case "3":
                    Console.WriteLine("谢谢使用，再见！");
                    return;
                default:
                    Console.WriteLine("请输入有效数字(1-3)！");
                    break;
            }
        }
    }
    
    static void Register()
    {
        Console.WriteLine("\n=== 用户注册 ===");
        
        string username;
        while (true)
        {
            Console.Write("请输入用户名: ");
            username = Console.ReadLine()?.Trim();
            
            if (string.IsNullOrWhiteSpace(username))
            {
                Console.WriteLine("用户名不可为空！");
                continue;
            }
            
            if (username.Length < 3 || username.Length > 15)
            {
                Console.WriteLine("用户名长度必须为3-15个字符！");
                continue;
            }
            
            if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
            {
                Console.WriteLine("用户名只能包含字母、数字、下划线！");
                continue;
            }
            
            if (users.ContainsKey(username))
            {
                Console.WriteLine("用户名已存在！");
                continue;
            }
            
            break;
        }
        
        string password;
        while (true)
        {
            Console.Write("请输入密码: ");
            password = Console.ReadLine();
            
            if (string.IsNullOrWhiteSpace(password))
            {
                Console.WriteLine("密码不可为空！");
                continue;
            }
            
            if (password.Length < 6 || password.Length > 20)
            {
                Console.WriteLine("密码长度必须为6-20个字符！");
                continue;
            }
            
            if (!Regex.IsMatch(password, @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$"))
            {
                Console.WriteLine("密码必须包含至少1个大写字母、1个小写字母和1个数字！");
                continue;
            }
            
            Console.Write("请确认密码: ");
            string confirmPassword = Console.ReadLine();
            
            if (password != confirmPassword)
            {
                Console.WriteLine("两次输入的密码不一致！");
                continue;
            }
            
            break;
        }
        
        users[username] = password;
        Console.WriteLine("注册成功！");
    }
    
    static void Login()
    {
        Console.WriteLine("\n=== 用户登录 ===");
        
        Console.Write("请输入用户名: ");
        string username = Console.ReadLine()?.Trim();
        
        Console.Write("请输入密码: ");
        string password = Console.ReadLine();
        
        if (!users.ContainsKey(username))
        {
            Console.WriteLine("用户名不存在！");
            return;
        }
        
        if (users[username] != password)
        {
            Console.WriteLine("密码错误！");
            return;
        }
        
        Console.WriteLine("登录成功！欢迎进入系统！");
        // 这里可以添加登录后的功能
        SystemHome(username);
    }
    
    static void SystemHome(string username)
    {
        Console.WriteLine($"\n=== 欢迎 {username} 进入系统 ===");
        Console.WriteLine("按任意键返回主菜单...");
        Console.ReadKey();
    }
}


    

    
           
    

    


    
    

            
        
        
    
    
   
    
